package com.trs.util;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.EnumSet;
import java.util.List;
import java.util.zip.GZIPInputStream;

import com.hierynomus.msdtyp.AccessMask;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.mssmb2.SMB2CreateDisposition;
import com.hierynomus.mssmb2.SMB2CreateOptions;
import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.smbj.share.DiskShare;

public class Txt {

	public static void main(String[] args) {

		try {
			System.err.println(readGzFile("D:\\Desktop\\20240714203419_8256_2773962796_msg.json.gz"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// SMBClient client = null;
		// Session session = null;
		// DiskShare diskShare = null;
		// Connection connection = null;
		// AuthenticationContext authenticationContext = null;
		//
		// try {
		// client = new SMBClient();
		// connection = client.connect("172.16.102.238");
		// // 创建连接会话.
		// authenticationContext = new AuthenticationContext("sunzhihuan", "qwer1234!".toCharArray(), "domain");
		// session = connection.authenticate(authenticationContext);
		// // 操作共享文件.
		// diskShare = (DiskShare) session.connectShare("share");
		// String fatherPath = diskShare.getSmbPath().toString().replace("\\", "/");
		// String sharePath = "";
		// List<String> list = new ArrayList<>();
		// ReadNasFepkRecurisive(fatherPath, sharePath, list, diskShare);
		// System.err.println(list);
		// } catch (Exception ex) {
		// ex.printStackTrace();
		// } finally {
		// try {
		// if (null != diskShare) {
		// diskShare.close();
		// }
		// if (null != session) {
		// session.close();
		// }
		// if (null != client) {
		// client.close();
		// }
		// } catch (Exception ex) {
		// }
		// }

	}

	// 递归调用
	public static void ReadNasFepkRecurisive(String fatherPath, String sharePath, List<String> filelist, DiskShare share) {
		List<FileIdBothDirectoryInformation> list = share.list(sharePath);
		for (FileIdBothDirectoryInformation file : list) {
			String filename = file.getFileName();
			if (".".equals(filename) || "..".equals(filename)) {
				continue;
			}
			if (EnumWithValue.EnumUtils.isSet(file.getFileAttributes(), FileAttributes.FILE_ATTRIBUTE_DIRECTORY)) {
				// 说明是文件夹
				String sonPath = fatherPath + "/" + filename;
				// 下一级路径
				sharePath = sharePath == "" ? filename : sharePath + "\\" + filename;
				// 递归读取
				ReadNasFepkRecurisive(sonPath, sharePath, filelist, share);
				// 读取完后恢复上一级路径
				if (sharePath.lastIndexOf("\\") != -1) {
					sharePath = sharePath.substring(0, sharePath.lastIndexOf("\\"));
				} else {
					sharePath = "";
				}
			} else {
				// 说明是文件
				StringBuffer strBuffer = new StringBuffer();
				String encoding = "UTF-8"; // 文本编码
				filelist.add(fatherPath + "/" + filename);
				com.hierynomus.smbj.share.File jsonfile = share.openFile(filename, EnumSet.of(AccessMask.GENERIC_READ), EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL), SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN_IF, EnumSet.noneOf(SMB2CreateOptions.class));
				InputStreamReader read;
				try {
					read = new InputStreamReader(jsonfile.getInputStream(), encoding);
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					while ((lineTxt = bufferedReader.readLine()) != null) {
						strBuffer.append(lineTxt);
					}
					read.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.err.println(strBuffer);
			}
		}
	}

	/**
	 * 打印日志
	 * 
	 * @param fileName
	 *            文件地址
	 * @param content
	 *            文件内容
	 */
	public static String write(String filePath, String content) {

		File file = new java.io.File(filePath);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}

		OutputStream os = null;
		try {
			os = new FileOutputStream(filePath, true);
			os.write(content.getBytes("UTF-8"));
			os.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return content;
	}

	/**
	 * 打印日志
	 * 
	 * @param fileName
	 *            文件地址
	 * @param content
	 *            文件内容
	 */
	public static String writeClean(String filePath, String content) {

		File file = new File(filePath);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}

		OutputStream os = null;
		try {
			os = new FileOutputStream(filePath);
			os.write(content.getBytes("UTF-8"));
			os.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return content;
	}

	/**
	 * 读取日志
	 * 
	 * @param fileName
	 *            文件地址
	 * @return 文件内容字符串
	 */
	public static String read(String filePath) {

		StringBuffer strBuffer = new StringBuffer();
		try {
			String encoding = "UTF-8"; // 文本编码
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					strBuffer.append(lineTxt + "\t");
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return String.valueOf(strBuffer);
	}

	/**
	 * 读取日志
	 * 
	 * @param fileName
	 *            文件地址
	 * @return 文件内容字符串
	 * @throws FileNotFoundException
	 * @throws UnsupportedEncodingException
	 */
	public static String read1(String filePath) throws Exception {

		StringBuffer strBuffer = new StringBuffer();
		String encoding = "UTF-8"; // 文本编码
		File file = new File(filePath);
		if (file.isFile() && file.exists()) { // 判断文件是否存在
			InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
			BufferedReader bufferedReader = new BufferedReader(read);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				strBuffer.append(line);
			}
			read.close();
		}

		return String.valueOf(strBuffer);
	}

	public static String readGzFile(String filePath) throws Exception {
		File file = new File(filePath);
		if (file.isFile() && file.exists()) { // 判断文件是否存在
			try (FileInputStream iFileInputStream = new FileInputStream(file); InputStream inputStream = new GZIPInputStream(iFileInputStream); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
				StringBuilder stringBuilder = new StringBuilder();
				String line;
				while ((line = reader.readLine()) != null) {
					stringBuilder.append(line);
				}
				return stringBuilder.toString();
			} catch (EOFException e) {
			}
			file.delete();
		}

		return "";
	}
}
